#ifndef SERVICE_POOL_H
#define SERVICE_POOL_H

#include <boost/asio.hpp>
#include <vector>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread/thread.hpp>

namespace tsc
{
namespace common
{

//! A pool of io_service objects.
class ServicePool : private boost::noncopyable
{
public:
  //! Construct the io_service pool.
  explicit ServicePool();

  //! Run all threads in pool.
  void run();

  //! Stop all io_service objects in the pool.
  void stop();

  //! Get an io_service to use.
  boost::asio::io_service& getService();

  //! Add a thread to pool.
  void addTread(boost::thread* pThread);

protected:

private:
  typedef boost::shared_ptr<boost::asio::io_service>       io_service_ptr;
  typedef boost::shared_ptr<boost::asio::io_service::work> work_ptr;
  typedef boost::shared_ptr<boost::thread>                 thread_ptr;

  //! The pool of io_services.
  std::vector<io_service_ptr> mIoServices;

  //! The work that keeps the io_services running.
  std::vector<work_ptr> mWork;

  //! Pool of threads;
  std::vector<thread_ptr> mThreads;
};

} // namespace common
} // namespace tsc

#endif // SERVICE_POOL_H
